package com.whksoft.mymapplugin.Listener;

import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerSwapHandItemsEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;

import static com.whksoft.mymapplugin.utils.flagUtils.haveQRFlag;

/**
 * 检测玩家切换到副手
 */
public class offHand implements Listener {

    Plugin plugin;

    public offHand(Plugin plugin) {
        this.plugin = plugin;
    }

    //检测玩家将工具切换到副手(1.9+)
    // FIXME: 2021/1/27 通过E不会被检测
    @EventHandler(priority = EventPriority.HIGHEST)
    public void swapItem(PlayerSwapHandItemsEvent e) {
        NamespacedKey key = new NamespacedKey(plugin, "isQRCode");
        ItemStack main = e.getOffHandItem();
        if (main == null) return;
        if (main.getType() == Material.FILLED_MAP) {
            if (haveQRFlag(key, main)) {
                e.setCancelled(true);
            }
        }
    }
}
